class Line < ActiveRecord::Base
  belongs_to :order, :foreign_key => 'order_num', :class_name => 'Order'
  
  validates_presence_of :product, :qty, :price, :unitcost
  validates_numericality_of :qty, :price, :unitcost
  
  def before_save
    self.sum_sales = self.qty * self.price
    self.sum_cost = self.qty * self.unitcost    
  end
  
  def after_save
    update_order
  end
  
  def after_destroy
    update_order
  end
  
  private 
  def update_order
    Order.find_by_order_num(self.order_num).save
  end
end
